<?php
class SupplierAction extends CommonAction {

    public function index()
    {
        $supplier = M('Supplier');
        parent::page($supplier->count());

        $data = $supplier
            ->alias('a')
            ->field(array('a.id', 'a.supplier_name', 'a.supplier_price', 'a.up_time', 'a.add_user', 'b.type_name'))
            ->join('ct_type b on a.supplier_type=b.id')
            ->order('a.up_time desc')
            ->limit($this->limit)
            ->select();

        $this->assign('data', $data);
        $this->display();
    }

    public function add()
    {
        $supplier = M('Supplier');
        $this->getType();

        if(isset($_POST['send']))
        {
            $this->validate(self::$ADD_SEC);
            $data['supplier_name'] = $this->_post('supplier_name');
            $data['supplier_type'] = $this->_post('supplier_type');
            $data['supplier_price'] = $this->_post('supplier_price');
            $data['supplier_ps'] = $this->_post('supplier_ps');
            $data['add_user'] = session('admin');
            $data['add_time'] = getDateTime();
            $data['up_time'] = getDateTime();

            if($supplier->add($data))
            {
                $type = M('Type');
                $currentType = $type->field('type_name')->where('id='.$data['supplier_type'])->find();
                $content = "用户：".session('admin')." 添加了一个供应商：$data[supplier_name] 供货种类为: $currentType[type_name] 价格：$data[supplier_price]  说明：$data[supplier_ps] ";
                addAdminRecord($content);
                redirectByModal('添加供应商成功!', self::$SUCCESS, __URL__ . '/index');
            }
        }
        $this->display();
    }

    public function update($id)
    {
        $supplier = M('Supplier');
        $currentData = $supplier->field(array('id', 'supplier_name', 'supplier_price', 'supplier_type', 'supplier_ps'))
                                ->where('id='.$id)
                                ->find();
        $this->getType();
        if(isset($_POST['send']))
        {
            $this->validate(self::$UPDATE_SEC);
            $data['supplier_name'] = $this->_post('supplier_name');
            $data['supplier_type'] = $this->_post('supplier_type');
            $data['supplier_price'] = $this->_post('supplier_price');
            $data['supplier_ps'] = $this->_post('supplier_ps');
            $data['up_time'] = getDateTime();
            $data['id'] = $this->_post('id');
            $content =
                "用户：".session('admin')." 修改了一个供应商：$currentData[supplier_name] 为 $data[supplier_name]
                供货种类为: $currentData[supplier_type] 为 $data[supplier_type]
                价格：$currentData[supplier_price] 为 $data[supplier_price]
                说明：$currentData[supplier_ps] 为 $data[supplier_ps]";
            $supplier->save($data);
            addAdminRecord($content);
            redirectByModal('修改供应商成功!', self::$SUCCESS, __URL__.'/index');
        }
        $this->assign('data', $currentData);
        $this->display();
    }


    public function delete()
    {
        $id = $this->_post('id');
        $supplier = M('Supplier');
        $data = $supplier->where('id='.$id)->find();
        if($supplier->where('id='.$id)->delete())
        {
            $type = M('Type');
            $currentType = $type->field('type_name')->where('id='.$data['supplier_type'])->find();
            $content = "用户：".session('admin')." 删除了一个供应商：$data[supplier_name] 供货种类为: $currentType[type_name] 价格：$data[supplier_price]  说明：$data[supplier_ps] ";
            addAdminRecord($content);
            redirectByModal('删除种类成功!', self::$SUCCESS, __URL__ . '/index');
        }
    }

    // 验证
    private function validate($sec)
    {
        $supplier_name = $this->_post('supplier_name');
        $supplier_type = $this->_post('supplier_type');
        $supplier_price = $this->_post('supplier_price');
        $supplier_ps = $this->_post('supplier_ps');

        if($supplier_name == '')
            redirectByModal('供应商名称必须填写!', self::$ERROR);
        else if($supplier_type == -1)
            redirectByModal('供货种类必须选择!', self::$ERROR);
        else if($supplier_price == '')
            redirectByModal('供应商价格必须选填写!', self::$ERROR);
        else if(!is_numeric($supplier_price))
            redirectByModal('供应商价格必须是数字!', self::$ERROR);
        else if(mb_strlen($supplier_ps, 'utf-8') > 255)
            redirectByModal('供应商说明最大为255位!', self::$ERROR);
        else if($sec == 'add')
        {
            $supplier = M('Supplier');
            if($supplier->where("supplier_name='$supplier_name'")->find())
                redirectByModal('供应商名称已经存在!', self::$ERROR);
        }
    }

    // 获取供货种类
    private function getType()
    {
        $type = M('Type');
        $typeData = $type->field(array('id', 'type_name'))->select();
        $this->assign('typeData', $typeData);
    }

}